package com.huishu.tcm.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import com.huishu.tcm.entity.Annex2;
import com.huishu.tcm.entity.Annex4;
import com.huishu.tcm.service.Annex2Service;
import com.huishu.tcm.service.Annex4Service;
import com.huishu.tcm.util.Constants;
import com.huishu.tcm.util.exception.TcmException;
import com.huishu.tcm.util.exception.TcmExceptionEnum;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huishu.tcm.entity.User;
import com.huishu.tcm.service.UserService;
import com.huishu.tcm.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

@Api(tags = "用户表")
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController<UserService> {

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询一条数据", notes = "根据主键查询一条数据")
    @RequestMapping(method = RequestMethod.GET)
    public User get(@RequestParam String id) throws TcmException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return service.getOne(wrapper);
    }

    /**
     * 查询数据列表
     * @return
     */
    @ApiOperation(value = "根据筛选条件查询数据列表", notes = "根据筛选条件查询数据列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public List<User> list(@RequestBody User v) throws TcmException {
        QueryWrapper<User> queryWrapper = service.getQueryWrapper(v);
        return service.list(queryWrapper);
    }

    /**
     * 分页查询数据列表
     * @param pageVo
     * @return
     */
    @ApiOperation(value = "根据筛选条件分页查询数据列表", notes = "根据筛选条件分页查询数据列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Page<User> page(@RequestBody PageVo<User> pageVo) throws TcmException {
        User v = pageVo.getEntity();
        QueryWrapper<User> wrapper = service.getQueryWrapper(v);
        Page<User> page = new Page<>(pageVo.getPage(),pageVo.getLimit());
        page = service.page(page,wrapper);
        return page;
    }

    /**
     * 添加
     * @param v
     * @return
     */
    @ApiOperation(value = "添加一条数据",notes = "添加一条数据")
    @RequestMapping(method = RequestMethod.POST)
    public boolean save(@RequestBody User v) throws Exception {
        return service.save(v);
    }

    /**
     * 根据主键更新
     * @param v
     * @return
     */
    @ApiOperation(value = "根据主键更新数据",notes = "根据主键更新数据")
    @RequestMapping(method = RequestMethod.PUT)
    public boolean update(@RequestBody User v) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", v.getId());
        return service.update(v,queryWrapper);
    }


    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键删除数据",notes = "根据主键删除数据")
    @RequestMapping(method = RequestMethod.DELETE)
    public boolean removeById(@RequestParam Integer id){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return service.remove(wrapper);
    }


    @Resource
    Annex4Service annex4Service; // 身体测量及静脉血筛查记录表
    @Resource
    Annex2Service annex2Service; // 糖尿病前期患者生活方式调查问卷


    @ApiOperation(value = "查询糖尿病前期患者生活方式调查问卷和身体测量及静脉血筛查记录表末次填写是否完成",
        notes = "返回值：{annex2:true、false, annex4:true、false}")
    @GetMapping(value = "/queryAnnex2And4TheLastTimeIsOver", params = {"patientId"})
    public Map<String, Boolean> queryAnnex2And4TheLastTimeIsOver(int patientId){
        QueryWrapper<Annex2> annex2QueryWrapper = new QueryWrapper<>();
        annex2QueryWrapper.eq("patient_id", patientId);
        annex2QueryWrapper.eq("the_last_time", true);
        Annex2 annex2 = annex2Service.getOne(annex2QueryWrapper);

        QueryWrapper<Annex4> annex4QueryWrapper = new QueryWrapper<>();
        annex4QueryWrapper.eq("patient_id", patientId);
        annex4QueryWrapper.eq("the_last_time", true);
        Annex4 annex4 = annex4Service.getOne(annex4QueryWrapper);

        Map<String, Boolean> map = new HashMap<>();
        map.put("annex2",false);
        map.put("annex4",false);
        if(ObjectUtil.isNotEmpty(annex2)){
            map.put("annex2", true);
        }
        if(ObjectUtil.isNotEmpty(annex4)){
            map.put("annex4", true);
        }
        return map;
    }

    /**
     * 结束患者管理
     * @param patientId 患者用户id
     */
    @ApiOperation(value = "结束患者管理", notes= "结束患者管理")
    @GetMapping(value = "/endManagementPatient", params = {"patientId"})
    public void endManagementPatient(int patientId){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", patientId);
        User user = service.getOne(queryWrapper);
        user.setEndManagement(true);
        service.update(user, queryWrapper);
    }

    /**
     * 未填表时取消提醒
     * @param patientId 患者用户id
     */
    @ApiOperation(value = "未填表时取消提醒", notes= "未填表时取消提醒")
    @GetMapping(value = "/notRemind", params = {"patientId"})
    public void notRemind(int patientId){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", patientId);
        User user = service.getOne(queryWrapper);
        user.setNotRemind(true);
        service.update(user, queryWrapper);
    }

    // 查询15天未填表的用户
    @ApiOperation(value = "查询15天未填表的用户列表", notes= "查询15天未填表的用户列表")
    @GetMapping(value = "/queryUser15DaysNotFilledAnnex", params = {"institutionId"})
    public List<User> queryUser15DaysNotFilledAnnex(int institutionId){
        Date day15Ago = new Date(new Date().getTime() - 15 * 24 * 60 * 60 * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("institution_id", institutionId);
        queryWrapper.ne("not_remind", true);
        queryWrapper.lt("create_time", sdf.format(day15Ago));
        return service.list(queryWrapper);
    }


    @ApiOperation(value = "审核医生注册信息", notes= "审核医生注册信息：通过or拒绝")
    @GetMapping(value = "/reviewedDoctorRegisterInfo", params = {"doctorId", "isPass"})
    public void reviewedDoctorRegisterInfo(int doctorId,boolean isPass) throws TcmException {
        // 判断登录用户是否为管理员用户
        User loginUser = getLoginUser();
        if(Constants.USER_ROLE_ADMIN.equals(loginUser.getRole())){
            User user = service.getById(doctorId);
            if(isPass){
                user.setRegisterStatus(Constants.USER_REGISTER_STATUS_success);
            }else{
                user.setRegisterStatus(Constants.USER_REGISTER_STATUS_failed);
            }
            service.updateById(user);
            return;
        }
        throw new TcmException(TcmExceptionEnum.USER_ACCOUN_NO_AUTHORITY);
    }
}
